package com.unitedvision.persistence.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.unitedvision.core.domain.GoodsType;

@Entity
@Table (name = "goods_type")
public class GoodsTypeEntity {
	private int id;
	private String name;
	private List<GoodsEntity> goods;

	public GoodsTypeEntity() {
		super();
	}

	public GoodsTypeEntity(GoodsType goodsType) {
		super();
		this.id = goodsType.getId();
		this.name = goodsType.getName();
	}

	public GoodsTypeEntity(String name) {
		super();
		this.name = name;
	}

	@Id @GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column (name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@OneToMany (targetEntity = GoodsEntity.class, mappedBy = "type", fetch = FetchType.LAZY)
	public List<GoodsEntity> getGoods() {
		return goods;
	}

	public void setGoods(List<GoodsEntity> goods) {
		this.goods = goods;
	}

	public GoodsType toGoodsType() {
		return new GoodsType(id, name);
	}
	
	public static GoodsTypeEntity create(String name) {
		return new GoodsTypeEntity(name);
	}
	
	public static GoodsTypeEntity create(GoodsType goodsType) {
		return new GoodsTypeEntity(goodsType);
	}

	@Override
	public String toString() {
		return "GoodsTypeEntity [id=" + id + ", name=" + name + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GoodsTypeEntity other = (GoodsTypeEntity) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
